package cn.tedu.charging.order.service;


import cn.tedu.charging.common.pojo.message.ChargingStartCheckMessage;
import cn.tedu.charging.common.pojo.message.DelayCheckMessage;
import cn.tedu.charging.common.pojo.param.OrderAddParam;
import cn.tedu.charging.common.pojo.vo.VehicleVO;
import cn.tedu.charging.common.protocol.JsonResult;
import cn.tedu.charging.common.utils.CronUtil;
import cn.tedu.charging.order.feign.DeviceClient;
import cn.tedu.charging.order.feign.UserClient;
import cn.tedu.charging.order.mqtt.MqttProducer;
import cn.tedu.charging.order.utils.XxlJobTaskUtil;
import com.alibaba.fastjson2.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.UUID;


@Service
@Slf4j
public class OrderService {

    @Autowired
    private DeviceClient deviceClient;
    @Autowired
    private UserClient userClient;
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private RestTemplate  restTemplate;
    @Autowired
    private MqttProducer mqttProducer;
    public String createOrder(OrderAddParam orderAddParam) {
       String orderNo= UUID.randomUUID().toString().replaceAll("-","");
       //1查枪是否可用
        checkGun(orderAddParam.getGunId());
        //2查用户是否绑定车辆
        checkVehicleAndGet(orderAddParam.getUserId());
        //3向订单发送消息告知设备开始自检充电
        String startCheckTopic="/charging/device/check/"+orderAddParam.getPileId();
        ChargingStartCheckMessage chargingStartCheckMessage = new ChargingStartCheckMessage();
        chargingStartCheckMessage.setOrderNo(orderNo);
        chargingStartCheckMessage.setUserId(orderAddParam.getUserId());
        chargingStartCheckMessage.setGunId(orderAddParam.getGunId());
        String messageJson = JSON.toJSONString(chargingStartCheckMessage);
        mqttProducer.sendMessage(startCheckTopic,messageJson);
        //4
        DelayCheckMessage delayCheckMessage = new DelayCheckMessage();
        delayCheckMessage.setOrderNo(orderNo);
        delayCheckMessage.setUserId(orderAddParam.getUserId());
        delayCheckMessage.setPileId(orderAddParam.getPileId());
        delayCheckMessage.setGunId(orderAddParam.getGunId());
        delayCheckMessage.setVehicleId(17);
        //交换机可以是fanout 交换机是topic 让业务队列绑定路由 #
        rabbitTemplate.convertAndSend("DELAY_BILL_EX","",delayCheckMessage);
        log.debug("延迟消息已经发送,检查订单是否有结果:{}",delayCheckMessage);
        //定时任务
        Integer time=1000*60*2;
        String cron = CronUtil.delayCron(time);
        XxlJobTaskUtil.createJobTask(restTemplate,cron,3,orderNo,orderAddParam.getUserId());
        return orderNo;
    }

    private void checkVehicleAndGet(Integer userId) {
        JsonResult<VehicleVO> vehicleVOJsonResult = userClient.bindeVehicle(userId);
        VehicleVO data = vehicleVOJsonResult.getData();
        if (data==null&&data.getId()==null){
            log.error("用户[{}]未绑定车辆",userId);
            throw new RuntimeException("用户未绑定车辆");
        }
    }

    private void checkGun(Integer gunId) {
        deviceClient.checkGun(gunId);
        if(!deviceClient.checkGun(gunId).getData()){
            log.error("枪[{}]不可用",gunId);
            throw new RuntimeException("枪不可用");
        }
        log.info("枪[{}]可用",gunId);
    }
}
